package jmine.tec.datadigester.domain.fileAssoc;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import jmine.tec.persist.annotation.Alias;
import jmine.tec.persist.annotation.Comment;
import jmine.tec.persist.annotation.Comments;
import jmine.tec.persist.annotation.Constraint;
import jmine.tec.persist.annotation.Index;
import jmine.tec.persist.annotation.Indexes;

import org.hibernate.annotations.ForeignKey;

import bancosys.tec.component.Documentation;
import bancosys.tec.datadigester.fileregistry.domain.FileRegistry;
import bancosys.tec.persist.bussobj.PersistableBusinessObject;

/**
 * Classe abstrata para associações entre objetos de dominio e os arquivos de dados de importação que os alimentam.
 * 
 * @author piercio
 */
@Entity
@Table(name = "FILE_ASSOC")
@Alias("FILASC")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Constraint(suffix = "0")
@DiscriminatorColumn(name = "FILE_ASSOC_TYPE", discriminatorType = DiscriminatorType.INTEGER)
@Comment(table = "FILE_ASSOC", column = "FILE_ASSOC_TYPE", value = "IDENTIFICA O TIPO DA ASSOCIACAO.")
@SequenceGenerator(name = "SEQ_FILASC", sequenceName = "SEQ_FILASC")
@Documentation("ASSOCIACAO ENTRE OBJETOS DE DOMINIO E ARQUIVOS DE IMPORTACAO.")
public abstract class AbstractFileAssoc extends PersistableBusinessObject {

    private Long id;

    private List<FileRegistry> fileRegistries;

    /**
     * @return o valor do campo id
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_FILASC")
    @Column(name = "COD_FILE_ASSOC", nullable = false)
    @Documentation("CODIGO DA ASSOCIACAO DE ARQUIVO.")
    public Long getId() {
        return this.id;
    }

    /**
     * @param id o valor para o campo id
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @return o valor do campo fileRegistries
     */
    @ManyToMany
    @Indexes({ @Index(suffix = "JT_0", columns = "COD_FILE_REG"), @Index(suffix = "JT_1", columns = "COD_FILE_ASSOC") })
    @Comments({ @Comment(table = "FILE_ASSOC_FILE_REGISTRY", value = "REGISTRO DE ARQUIVOS ASSOCIADOS"),
            @Comment(table = "FILE_ASSOC_FILE_REGISTRY", column = "COD_FILE_ASSOC", value = "CODIGO DA ASSOCIACAO DE ARQUIVO."),
            @Comment(table = "FILE_ASSOC_FILE_REGISTRY", column = "COD_FILE_REG", value = "CÓDIGO DO REGISTRO DE IMPORTAÇÃO") })
    @JoinTable(name = "FILE_ASSOC_FILE_REGISTRY", joinColumns = { @JoinColumn(name = "COD_FILE_ASSOC") }, inverseJoinColumns = { @JoinColumn(name = "COD_FILE_REG") })
    @Alias("FASFRG")
    @ForeignKey(name = "FK_FILASC_FASFRG")
    public List<FileRegistry> getFileRegistries() {
        return this.fileRegistries;
    }

    /**
     * @param fileRegistries o valor para o campo fileRegistries
     */
    public void setFileRegistries(List<FileRegistry> fileRegistries) {
        this.fileRegistries = fileRegistries;
    }

    /**
     * Adiciona um registro de arquivo a associação.
     * 
     * @param fileRegistry o registro de arquivo de importação.
     */
    public void addFileRegistry(FileRegistry fileRegistry) {
        if (this.fileRegistries == null) {
            this.fileRegistries = new ArrayList<FileRegistry>();
        }
        this.fileRegistries.add(fileRegistry);
    }

}